﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Text;
using DataAccess;
namespace tracnghiemonline2
{
    public partial class Default : System.Web.UI.Page
    {
        public int check = 0;

        public List<int> month_id = new List<int>();
        public List<int> month_score = new List<int>();
        public List<int> total_id = new List<int>();
        public List<int> total_score = new List<int>();

        public string date = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["level"] != null)
                check = int.Parse(Session["level"].ToString());

            var get_rank_total = (from p in new TblRankTotalController().FetchAll() select p).ToList();

            int rank_id = 0;
            for (int i = 0; i < get_rank_total.Count; i++)
            {
                var get_user = new TblUserController().FetchAll().Where(x => x.UserId == get_rank_total[i].UserID).Single();
                if (get_user.Active == false)
                    continue;
                if (checkTopTotal(get_user.UserId) == -1)
                {
                    total_id.Add(int.Parse(get_rank_total[i].UserID.ToString()));
                    total_score.Add(int.Parse(get_rank_total[i].ScoreTotal.ToString()));
                    rank_id++;
                }
                else
                {
                    total_score[checkTopTotal(get_user.UserId)] += int.Parse(get_rank_total[i].ScoreTotal.ToString());
                }
            }
            sortTopTotal();
            var get_rank_month = new TblRankMonthController().FetchAll().ToList();
            if (get_rank_month.Count == 0)
            {
                new TblRankMonthController().Insert(DateTime.Now);
            }
            else
            {
                var get_last = new TblRankMonthController().FetchAll().Last();
                int month = DateTime.Parse(get_last.DateX.ToString()).Month;
                int year = DateTime.Parse(get_last.DateX.ToString()).Year;

                if (month != DateTime.Now.Month || year != DateTime.Now.Year)
                {
                    new TblRankMonthController().Insert(DateTime.Now);
                }
            }

            var get_rank_month_last = new TblRankMonthController().FetchAll().Last();
            date = DateTime.Parse(get_rank_month_last.DateX.ToString()).Month + "/" + DateTime.Parse(get_rank_month_last.DateX.ToString()).Year;
            var get_rank_month_score = new TblRankMonthScoreController().FetchAll().Where(x => x.RankDate == get_rank_month_last.Id).ToList();
            int get_top_month = 0;
            for (int i = 0; i < get_rank_month_score.Count; i++)
            {
                var get_user = new TblUserController().FetchAll().Where(x => x.UserId == get_rank_month_score[i].UserID).Single();
                if (get_user.Active == false)
                    continue;
                if (checkTopMonth(get_user.UserId) == -1)
                {
                    month_id.Add(int.Parse(get_rank_month_score[i].UserID.ToString()));
                    month_score.Add(int.Parse(get_rank_month_score[i].Score.ToString()));
                    get_top_month++;
                }
                else
                {
                    month_score[checkTopMonth(get_user.UserId)] += int.Parse(get_rank_month_score[i].Score.ToString());
                }
            }

            sortTopMonth();

        }
        public static string convertToUnSign(string s)
        {
            string convert = "ĂÂÀẰẦÁẮẤẢẲẨÃẴẪẠẶẬỄẼỂẺÉÊÈỀẾẸỆÔÒỒƠỜÓỐỚỎỔỞÕỖỠỌỘỢƯÚÙỨỪỦỬŨỮỤỰÌÍỈĨỊỲÝỶỸỴĐăâàằầáắấảẳẩãẵẫạặậễẽểẻéêèềếẹệôòồơờóốớỏổởõỗỡọộợưúùứừủửũữụựìíỉĩịỳýỷỹỵđ\".:;'<>?~!@#$%^&*()_+-=\\/*,";
            string To = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyydaaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyyd                           ";
            for (int i = 0; i < To.Length; i++)
            {
                s = s.Replace(convert[i], To[i]);
            }
            return s;
        }

        public void sortTopTotal()
        {
            for (int i = 0; i < total_id.Count-1; i++)
            {
                for (int j = i + 1; j < total_id.Count; j++)
                {
                    int check1 = total_score[i];
                    int check2 = total_score[j];
                    if (check1 < check2)
                    {
                        int id = total_id[i];
                        int score = total_score[i];

                        total_id[i] = total_id[j];
                        total_score[i] = total_score[j];

                        total_id[j] = id;
                        total_score[j] = score;
                    }
                }
            }
        }

        public void sortTopMonth()
        {
            for (int i = 0; i < month_id.Count-1; i++)
            {
                for (int j = i + 1; j < month_id.Count; j++)
                {
                    if (month_score[i] < month_score[j])
                    {
                        int id = month_id[i];
                        int score = month_score[i];

                        month_id[i] = month_id[j];
                        month_score[i] = month_score[j];

                        month_id[j] = id;
                        month_score[j] = score;
                    }
                }
            }
        }

        public static string convertToUrl(string s)
        {
            string url = "";
            s = s.ToLower();
            if (s.IndexOf(' ') != -1)
            {
                string[] split = new string[s.Split(' ').Length];
                split = s.Split(' ');

                for (int i = 0; i < split.Length; i++)
                {
                    url += convertToUnSign(split[i].Trim());
                    if (i < split.Length - 1)
                        url += "-";
                }
                s = url;
            }
            else
            {
                url += convertToUnSign(s);
            }
            s = url;
            return s;
        }
        public bool checkLevel(string s)
        {
            string[] split = new string[s.Split(',').Length];
            split = s.Split(',');

            for (int i = 0; i < s.Split(',').Length; i++)
            {
                if (split[i] == check.ToString())
                    return true;
            }
            return false;
        }
        public int getMaxScore(int id)
        {
            int max = 0;
            var get_result = new TblResultController().FetchAll().Where(x => x.TestId == id && x.ResultUser == int.Parse(Session["user_id"].ToString()));
            var get_td = new TblTestDetailController().FetchAll().Where(x => x.TestId == id).ToList();
            foreach (TblResult result in get_result)
            {
                int score = (100 * int.Parse(result.ResultCorrect.ToString())) / get_td.Count;
                if (score > max)
                    max = score;
            }
            return max;
        }

        int checkTopTotal(int id)
        {
            for (int i = 0; i < total_id.Count; i++)
            {
                if (total_id[i] == id)
                    return i;
            }
            return -1;
        }

        int checkTopMonth(int id)
        {
            for (int i = 0; i < month_id.Count; i++)
            {
                if (month_id[i] == id)
                    return i;
            }
            return -1;
        }
        int checkExitsUser(int[] a, int id)
        {
            int z = -1;
            for (int i = 0; i < 10; i++)
            {
                if (a[i] == id)
                    return i;
            }
            return z;
        }
    }
}